﻿using System;
using System.Drawing;
using System.Drawing.Imaging;
using System.Diagnostics;

namespace iPhoneUI
{
    /// <summary>
    /// This class provides a layer of abstraction between the widgets and a graphics object.
    /// This allows the coordinate system of widgets to be shifted prior to rendering leaving
    /// the widgets themselves blissfully ignorant
    /// </summary>
    public class Canvas
    {
        private Graphics m_graphics;
        private Point m_shiftVector;    // vector used to shift paint operations from widget space to form space

        private Canvas()
        {
        }

        public Canvas(Graphics graphics)
            : this(graphics, Point.Empty)
        {
        }

        public Canvas(Graphics graphics, Point shiftVector)
        {
            Debug.Assert(graphics != null);
            m_graphics = graphics;
            m_shiftVector = shiftVector;
        }

        public SizeF MeasureString(string text, Font font)
        {
            return m_graphics.MeasureString(text, font);
        }

        public void DrawString(string text, Font font, Brush brush, RectangleF layoutRectangle, StringFormat format)
        {
            layoutRectangle.X += m_shiftVector.X;
            layoutRectangle.Y += m_shiftVector.Y;

            m_graphics.DrawString(text, font, brush, layoutRectangle, format);
        }

        public void DrawString(string text, Font font, Brush brush, RectangleF layoutRectangle)
        {
            layoutRectangle.X += m_shiftVector.X;
            layoutRectangle.Y += m_shiftVector.Y;

            m_graphics.DrawString(text, font, brush, layoutRectangle);
        }

        public void DrawString(string text, Font font, Brush brush, int x, int y)
        {
            m_graphics.DrawString(text, font, brush, x + m_shiftVector.X, y + m_shiftVector.Y);
        }

        public void DrawIcon(Icon icon, int x, int y)
        {
            m_graphics.DrawIcon(icon, x + m_shiftVector.X, y + m_shiftVector.Y);
        }

        public void DrawImage(Image image, Rectangle destRect, int srcX, int srcYy, int srcWidth, int srcHeight, ImageAttributes attributes)
        {
            destRect.X += m_shiftVector.X;
            destRect.Y += m_shiftVector.Y;

            m_graphics.DrawImage(image, destRect, srcX, srcYy, srcWidth, srcHeight, GraphicsUnit.Pixel, attributes);
        }

        public void DrawImage(Image image, Rectangle destRect, Rectangle srcRect)
        {
            destRect.X += m_shiftVector.X;
            destRect.Y += m_shiftVector.Y;

            m_graphics.DrawImage(image, destRect, srcRect, GraphicsUnit.Pixel);
        }

        public void AlphaBlend(Image image, byte sourceConstantAlpha, int x, int y)
        {
            m_graphics.AlphaBlend(image, sourceConstantAlpha, x + m_shiftVector.X, y + m_shiftVector.Y);
        }

        public void AlphaBlend(Image image, byte sourceConstantAlpha, Rectangle destRect, Rectangle srcRect)
        {
            destRect.X += m_shiftVector.X;
            destRect.Y += m_shiftVector.Y;

            m_graphics.AlphaBlend(image, sourceConstantAlpha, destRect, srcRect);
        }

        public void AlphaBlend(IImage image, Rectangle destRect)
        {
            destRect.X += m_shiftVector.X;
            destRect.Y += m_shiftVector.Y;

            m_graphics.AlphaBlend(image, destRect);
        }

        public void DrawRectangle(Pen pen, Rectangle rect)
        {
            rect.X += m_shiftVector.X;
            rect.Y += m_shiftVector.Y;

            m_graphics.DrawRectangle(pen, rect);
        }
    }
}
